import BaseAssetLoad from "./BaseAssetLoad";
import MsgDispatcher, { MsgType } from "../../Tool/MsgDispatcher";
import { EventTool } from "../../Tool/EventTool";
import MathTool from "../../Tool/MathTool";
import GameAdsManager from "../../Ad/GameAdsManager";

export default class QQAssetLoader extends BaseAssetLoad 
{
    
    private m_packages:Array<string>=["res","ui","ParticleScene"];
    private m_successCount:number=0;
    private m_successCallback:Function=null;
    private m_completeCallback:Function=null;
    private m_failCallback:Function=null;

    public OnLoadFinished() 
    {
        EventTool.LogEvent("资源加载完毕");
    }


    public OnLoadPregress(percent: number) 
    {
        MsgDispatcher.Send(MsgType.GameLoading.toString(), percent);
    }


    public LoadAsset(succss?:Function,fail?:Function,complete?:Function) 
    {
        this.m_successCallback=succss;
        this.m_failCallback=fail;
        this.m_completeCallback=complete;
        this.m_successCount=0;

        if ( Laya["QQMiniAdapter"] != null && Laya["QQMiniAdapter"].nativefiles != null) 
        {

            this.LoadSubPackage(this.m_packages[this.m_successCount]);
        }
    }


     //图集加载后回调
     private  LoadSubPackage(packageName:string):void
     {
        // UIManager.Instance.PushPanel(ViewType.LoadingView);
         //小游戏官方的分包加载方式
         const loadcallback=  qq.loadSubpackage({
             name: packageName, // name 可以填 name 或者 root
             success: (res)=>
             {
                 //分包加载成功后通过 success 回调
                 console.log("success "+packageName);
                 this.OnLoadOver();
             },
             fail: (res)=> {
                 // 分包加载失败通过 fail 回调
                 this.m_failCallback&&this.m_failCallback();
                 console.log("fail");
             },complete:(res)=>{
                this.m_completeCallback&&this.m_completeCallback();
                 console.log("complete");
             }
         });     
     }


     private OnLoadOver()
     {
        this.m_successCount++;
        if (this.m_successCount>=this.m_packages.length) 
        {
            this.m_successCallback&&this.m_successCallback();
        }
        else{
            this.LoadSubPackage(this.m_packages[this.m_successCount]);
        }
     }


}